﻿using System;
using System.Web;
using System.Web.Security;
using Jayden.Dll.Core;
using Jayden.Dll.Core.Logging;
using Jayden.Dll.Core.Membership;
using Jayden.Dll.Core.Persistence;
using Jayden.Dll.Core.Translations;
using Jayden.Dll.Web.Enums;
using Jayden.Dll.Web.Events;
using Jayden.Dll.Web.HtmlControls;
using Jayden.Dll.Web.Json;
using Jayden.Dll.Web.Scripts;
using Jayden.Dll.Web.Styles;

namespace Jayden.Dll.Web
{
	public class HttpHandlerContext : IDisposable
	{
		private HttpHandler m_Handler;
		private Configuration m_Configuration;
		private Uri m_Uri;
		private UriAccess m_UriAccess;
		private UnitOfWork m_UnitOfWork;
		private HttpRequestType m_RequestType;
		private HttpContext m_Context;
		private HtmlControlLoader m_ControlLoader;
		private RenderMode m_RenderMode;
		private ITranslator m_Translator;
		private Html m_Html;
		private JsonObject m_Json;
		private JsFile m_Js;
		private CssFile m_Css;
		private HttpEventHandlerManager m_HandlerManager;
		private Logger m_Logger;
		private UserAgent m_UserAgent;
		private User m_User;

		public HttpHandlerContext(HttpHandler handler, Configuration config, Logger logger, Uri uri, UriAccess uriAccess, HttpRequestType type, HttpContext context, RenderMode renderMode, ITranslator translator, Html html, JsonObject json, JsFile js, CssFile css, HttpEventHandlerManager handlerManager, HtmlControlLoader loader)
		{
			m_Handler = handler;
			m_Configuration = config;
			m_Logger = logger;
			m_Uri = uri;
			m_UriAccess = uriAccess;
			m_RequestType = type;
			m_Context = context;
			m_RenderMode = renderMode;
			m_Translator = translator;
			m_Html = html;
			m_Json = json;
			m_Js = js;
			m_Css = css;
			m_ControlLoader = loader;
			m_HandlerManager = handlerManager;
			m_UserAgent = new UserAgent(context.Request.UserAgent);
			if (context != null)
			{
				context.Items["HttpHandlerContext"] = this;
				context.Items["UnitOfWork"] = UnitOfWork;
			}
			if (context.User.Identity.IsAuthenticated)
				m_User = Membership.GetUser() as User;
		}

		public HttpHandler Handler { get { return m_Handler; } }
		public HttpRequest Request { get { return m_Context.Request; } }
		public HttpRequestType RequestType { get { return m_RequestType; } }
		public HttpContext Context { get { return m_Context; } }
		public UserAgent UserAgent { get { return m_UserAgent; } }
		public RenderMode RenderMode { get { return m_RenderMode; } }
		public ITranslator Translator { get { return m_Translator; } }
		public string Lang { get { return Translator.Lang; } }
		public Html Html { get { return m_Html; } }
		public JsonObject Json { get { return m_Json; } }
		public JsFile Js { get { return m_Js; } }
		public CssFile Css { get { return m_Css; } }
		public HttpEventHandlerManager HandlerManager { get { return m_HandlerManager; } }
		public Configuration Configuration { get { return m_Configuration; } }
		public Uri Uri { get { return m_Uri; } }
		public UriAccess UriAccess { get { return m_UriAccess; } }
		public Logger Logger { get { return m_Logger; } }
		public HtmlControlLoader ControlLoader { get { return m_ControlLoader; } }
		public bool IsAuthenticated { get { return m_Context.User.Identity.IsAuthenticated; } }
		public string UserName { get { return m_Context.User.Identity.Name; } }
		public User User { get { return m_User; } }

		public UnitOfWork UnitOfWork
		{
			get
			{
				if (m_UnitOfWork == null)
					m_UnitOfWork = new UnitOfWork(Configuration.GetConfiguration("UnitOfWork"));
				return m_UnitOfWork;
			}
		}

		public string MapPath(string virtualPath) { return Context.Server.MapPath(virtualPath); }

		public void Dispose()
		{
			m_UnitOfWork.Dispose();
			m_UnitOfWork = null;
		}

		public Mailer GetMailer(string maileId)
		{
			Configuration config = Configuration.GetConfiguration("Mailer");
			if (config != null)
			{
				string overrideFile = config.GetValue("@override");
				if (overrideFile != null)
				{
					overrideFile = MapPath(overrideFile);
					if (System.IO.File.Exists(overrideFile))
					{
						Configuration overrideConfig = new Configuration(overrideFile);
						config = overrideConfig;
					}
				}
				maileId = maileId ?? config.GetValue("@default", "Default");
				Configuration mailerConfig = config.GetConfiguration("Mailer[@id=\"" + maileId + "\"]");
				if(mailerConfig != null)
						return new Mailer(mailerConfig);
			}
			return null;
		}
	}
}
